<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Model extends MY_Controller
{

    public function index()
    {
        /*
         * $page = _get_page();
         * $pagesize = 3;
         * $arrParam = array();
         * $arrWhere = array('status'=>1,'b.photonum>'=>0);
         *
         * $this->load->model('User_model');
         * $this->load->model('Photo_model');
         * $dbprefix = $this->User_model->db->dbprefix;
         * $tb = $dbprefix.'user a inner join '.$dbprefix.'user_num b on(a.id=b.userid)';
         * $list = $this->User_model->fetch_page($page, $pagesize, $arrWhere,'a.id,a.nickname','a.addtime desc', $tb);
         *
         * $alist = array('rows'=>array());
         * foreach ($list['rows'] as $key => $value) {
         * $rs = $this->Photo_model->fetch_page(1, 5, array('userid'=>$value['id'],'status'=>1),'img');
         * $value['photolist'] = $rs['rows'];
         * $alist['rows'][]=$value;
         * }
         *
         * //分页
         * $pagecfg = array();
         * $pagecfg['base_url'] = _create_url('model', $arrParam);
         * $pagecfg['total_rows'] = $list['count'];
         * $pagecfg['cur_page'] = $page;
         * $pagecfg['per_page'] = $pagesize;
         * //$this->load->library('pagination');
         * $this->pagination->initialize($pagecfg);
         * $alist['pages'] = $this->pagination->create_links();
         *
         * $result = array(
         * 'list' => $alist,
         * );
         */
        $this->load->model('Style_model'); // 加载分类模型
        
        $get_style = (int) $this->input->get('style');
        $get_sex = (int) $this->input->get('sex');
        $get_area = $this->input->get('area');
        $get_height = $this->input->get('height');
        $get_orderby = $this->input->get('orderby');
        $get_city = $this->input->get('city');
        $user_label = $this->input->get('label');

        //搜索身高输入
        $min_height = $this->input->get('min_height');
        $max_height = $this->input->get('max_height');
        
        $page = _get_page();
        $pagesize = 100; // 每页大小
        $arrParam = array();
        $arrWhere = array(
            'a.usertype' => '1',
            'a.nickname<>' => "''",
            'a.showimg<>' => "''",
            'a.showimg2<>' => "''",
            '(a.status' => '1 or a.status=2)',
            'hangup_status'=>$this->show_hangup_status
        );
        if ($get_style) {
            $arrWhere["concat(',',style,',') like "] = "'%," . $get_style . ",%'";
            $arrParam['style'] = $get_style;
        }
        if ($get_sex) {
            $arrWhere['sex'] = $get_sex;
            $arrParam['sex'] = $get_sex;
        }
        if (isset($get_area)) {
        	$get_area = (int)$get_area;
            $arrWhere['a.area'] = $get_area;
            $arrWhere['a.user_label'] = "0";
            $arrParam['area'] = $get_area;
        }
        elseif (isset($user_label)) {
            $user_label = (int)$user_label;
            $arrWhere['a.user_label'] = $user_label;
            $arrParam['label'] = $user_label;
        }
        if ($get_city) {
            // 城市
            if ($get_city == '其他') {
                $cityStr = $this->Style_model->getCityStr();
                $arrWhere['city not in ('] = $cityStr . ')';
            } else {
                $arrWhere['(city'] = "'" . $get_city . "市' or city='" . $get_city . "')";
            }
            $arrParam['city'] = $get_city;
        }
        
        if ($get_height) {
            $arrHeightParam = explode('-', $get_height);
            if (count($arrHeightParam) == 2) {
                if ($arrHeightParam[0])
                    $arrWhere['height>='] = $arrHeightParam[0];
                if ($arrHeightParam[1])
                    $arrWhere['height<='] = $arrHeightParam[1];
            } else {
            	$get_height = (string)$get_height;
            	if (strstr($get_height, '*'))
            	{
            		$arrWhere['height>'] = str_replace('*', '',$get_height);
            	}
            	else if (strstr($get_height, '-'))
            	{
            		$arrWhere['height<'] = str_replace('-', '',$get_height);
            	}
            }
            $arrParam['height'] = $get_height;
        }
        else 
        {
        	if ($min_height && $max_height)
        	{
        		$arrWhere['height>='] = $min_height;
        		$arrWhere['height<='] = $max_height;
        	}
        	else if ($min_height)
        	{
        		$arrWhere['height>='] = $min_height;
        	}
        	else if ($max_height)
        	{
        		$arrWhere['height<='] = $max_height;
        	}
        	$arrParam['min_height'] = $min_height;
        	$arrParam['max_height'] = $max_height;
        }
        
        $orderby = 'visitnum desc,addtime desc';
        if ($get_orderby) {
            $arrParam['orderby'] = $get_orderby;
            switch ($get_orderby) {
                case 1:
                    $orderby = 'addtime desc';
                    break;
                case 2:
                    $orderby = 'fansnum desc';
                    break;
                case 3:
                    $orderby = 'be_ordernum desc';
                    break;
                
                default:
                    $orderby = 'addtime desc';
                    break;
            }
        }
        
        $this->load->model('User_model');
        $dbprefix = $this->User_model->db->dbprefix;
        $tb = $dbprefix . 'user a left join ' . $dbprefix . 'user_detail b on(a.id=b.userid) left join ' . $dbprefix . 'user_num c on(a.id=c.userid)';
        $list = $this->User_model->fetch_page($page, $pagesize, $arrWhere, 'a.id,a.nickname,a.showimg,a.showimg2,a.city,b.bust,b.waist,b.hips,b.shoes,b.height', $orderby, $tb);
//         echo $this->User_model->db->last_query();die;
        
        $this->load->model('Usernum_model');
        
        if (! empty($list['rows'])) {
            foreach ($list['rows'] as $key => $v) {
                $numInfo = $this->Usernum_model->get_info_by_userid($v['id']);
                $list['rows'][$key]['num_info'] = $numInfo;
            }
        }
        
        // echo $this->User_model->db->last_query();
        // 分页
        $pagecfg = array();
        $pagecfg['base_url'] = _create_url('model', $arrParam);
        $pagecfg['total_rows'] = $list['count'];
        $pagecfg['cur_page'] = $page;
        $pagecfg['per_page'] = $pagesize;
        // $this->load->library('pagination');
        $this->pagination->initialize($pagecfg);
        $list['pages'] = $this->pagination->create_links();
        
        // 风格
        $oSysModelstyle = $this->Style_model->getShowStyle();
        $oSysHight = $this->Style_model->getShowHight();
        $oCity = $this->Style_model->getShowCity();
        //模特地域分类
        $areaType = _get_config('area_type');
        
        $result = array(
            'list' => $list,
            'oSysModelstyle' => $oSysModelstyle,
            'arrParam' => $arrParam,
            'oHeight' => $oSysHight,
            'oCity' => $oCity,
        	'areaType' => $areaType,
        );
        
        $this->load->view('model', $result);
    }
}